package zunge.tool.db.sql;

import java.sql.*;


//Database Connections, SELECT only.
public class DbConn {
	public Connection conn;
	public PreparedStatement pstmt;
	public String db_url;
	public String db_url_withaccount;
	
	public DbConn(String host, String db_name, String user_name, String password) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		db_url = "jdbc:mysql://" + host + "/" + db_name;
	    
	    	db_url_withaccount = db_url + "?user="+user_name+
			((password!=null)?"&password="+password:"");
		System.out.println(db_url);
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		conn = DriverManager.getConnection(db_url_withaccount);

	}
	
	public void close() throws SQLException{
		conn.close();
	}
	
	public ResultSet executeQuery(String query) throws SQLException{
		pstmt = conn.prepareStatement(query);
		return pstmt.executeQuery();

	}

	public PreparedStatement getPreparedStatement(String query) throws SQLException{
		return conn.prepareStatement(query);
	}
	public int executeUpdate(PreparedStatement ps) throws SQLException{
		return ps.executeUpdate();
	}
	
}

